//
//  oldBalance.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/3/31.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldBalance.h"
#import "oldNetWork.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApiObject.h"
#import "WXApi.h"

@interface oldBalance()
@property (nonatomic, copy)NSString *balance;
@end

@implementation oldBalance

+ ( instancetype ) shareInstance
{
    static oldBalance * instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once( &onceToken, ^
                  {
                      instance = [self new];
                  });
    return instance;
}

NSString *GetPayType(PayType status) {
    switch (status) {
        case ALI_PAY:
            return @"AlipayMobileApp";
            break;
        case WECHAT_PAY:
            return @"WeixinMobileApp";
            break;
        default:
            return @"";
    }
}

- ( void ) requestBalanceSuccess : ( BalanceSuccessBlock ) successBlock
                           error : ( BalanceErrorBlock ) errorBlock
{
    WeakSelf
    
    [OLDNetWork httpRequestWithGet:URL_Pay_Balance params:nil success:^(id data) {
        if (data) {
            weakSelf.balance = data[@"balance"];
            successBlock(weakSelf.balance);
        }
    } error:^(NSString *error) {
        errorBlock(error);
    }];
}

- (void)AliPay:(NSString *)oid price:(NSString *)price type:(NSString *)type success:(BalanceSuccessBlock) successBlock error : ( BalanceErrorBlock ) errorBlock
{
    NSString *appScheme = @"yczylaonianbangfamily";
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys: price, @"amount", type, @"type", GetPayType(ALI_PAY), @"pay_method", nil];
    if (oid) {
        [param setObject:oid forKey:@"order_id"];
    }
    
    [OLDNetWork httpRequestWithPostPut:URL_Pay_Sign params:param success:^(id data) {
        if(data)
        {
            successBlock(data);
            [[AlipaySDK defaultService] payOrder:data[@"data"] fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                NSLog(@"reslut = %@",resultDic);
            }];
        }
    } error:^(NSString *error) {
        errorBlock(error);
    }];
}

- (void)WechatPay:(NSString *)oid price:(NSString *)price type:(NSString *)type success:(BalanceSuccessBlock) successBlock error : ( BalanceErrorBlock ) errorBlock
{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys: price, @"amount", type, @"type", GetPayType(WECHAT_PAY), @"pay_method", nil];
    if (oid) {
        [param setObject:oid forKey:@"order_id"];
    }
    
    [OLDNetWork httpRequestWithPostPut:URL_Pay_Sign params:param success:^(id data) {
        if(data)
        {
            successBlock(data);
            NSData* nsdata = [data[@"data"] dataUsingEncoding:NSUTF8StringEncoding];
            
            NSDictionary *data = [NSJSONSerialization JSONObjectWithData:nsdata options:NSJSONReadingAllowFragments error:nil];
            
            NSMutableString *retcode = [data objectForKey:@"return_code"];
            if (retcode.intValue == 0){
                NSMutableString *stamp  = [data objectForKey:@"timestamp"];
                
                //调起微信支付
                PayReq* req             = [[PayReq alloc] init];
                req.partnerId           = [data objectForKey:@"partnerid"];
                req.prepayId            = [data objectForKey:@"prepayid"];
                req.nonceStr            = [data objectForKey:@"noncestr"];
                req.timeStamp           = stamp.intValue;
                req.package             = [data objectForKey:@"package"];
                req.sign                = [data objectForKey:@"sign"];
                [WXApi sendReq:req];
                //日志输出
                NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[data objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
            }
        }
    } error:^(NSString *error) {
        errorBlock(error);
    }];
}

- (void)ConsumePay:(NSString *)oid success:(BalanceSuccessBlock) successBlock error : ( BalanceErrorBlock ) errorBlock
{
    if (!oid) {
        errorBlock(@"服务为空");
        return;
    }
    
    [OLDNetWork httpRequestWithPostPut:URL_Pay_Consume params:@{@"order_id":oid} success:^(id data) {
        successBlock(data);
    } error:^(NSString *error) {
        errorBlock(error);
    }];
}


- (NSString *)getBanlance
{
    return _balance;
}

@end
